﻿/************************************************************************************
 * xujiajun@yoozoo.com 御剑
 * 衔接Lua与TA提供的FogUtility的中间层，提供了在原基础数据上，往外扩充迷雾效果的功能
 ***********************************************************************************/

using UnityEngine;

public class FogSystem
{
    /// <summary>
    /// 外边大小
    /// </summary>
    private static int k_Margin;

    /// <summary>
    /// 测试Margin的例子
    /// </summary>
    public static void Test()
    {
        bool[] data = new bool[25] { true, true, true, true, true,
            true, false, false, false, true,
            true, false, true, false, true,
            true, false, false, false, true,
            true, true, true, true, true
        };
        Init(null, data, 5, 5);
    }

    /// <summary>
    /// 测试时使用的数据打印
    /// </summary>
    /// <param name="unlockedData"></param>
    /// <param name="mapSize"></param>
    private static void PrintData(bool[] unlockedData, int mapSize)
    {
        string str = "";
        int len = unlockedData.Length;
        for (int i = 0; i < len; i++)
        {
            str += i % mapSize == 0 ? "\n" : "";
            str += (unlockedData[i] ? 1 : 0) + ", ";
        }
        Debug.Log(str);
    }

    /// <summary>
    /// 测试时使用的数据打印
    /// </summary>
    /// <param name="unlockedData"></param>
    /// <param name="mapSize"></param>
    private static void PrintData(byte[] unlockedData, int mapSize)
    {
        string str = "";
        int len = unlockedData.Length;
        for (int i = 0; i < len; i++)
        {
            str += i % mapSize == 0 ? "\n" : "";
            str += unlockedData[i] + ", ";
        }
        Debug.Log(str);
    }

    /// <summary>
    /// 初始化迷雾系统。会根据外边大小对数据进行扩充
    /// 例如当margin = 2时
    /// 原数据如下：
    /// 1,1,1,1,1,
    /// 1,0,0,0,1,
    /// 1,0,1,0,1,
    /// 1,0,0,0,1,
    /// 1,1,1,1,1,
    /// 根据margin扩充后的数据如下：
    /// 0,0,0,0,0,0,0,0,0,
    /// 0,0,0,0,0,0,0,0,0,
    /// 0,0,1,1,1,1,1,0,0,
    /// 0,0,1,0,0,0,1,0,0,
    /// 0,0,1,0,1,0,1,0,0,
    /// 0,0,1,0,0,0,1,0,0,
    /// 0,0,1,1,1,1,1,0,0,
    /// 0,0,0,0,0,0,0,0,0,
    /// 0,0,0,0,0,0,0,0,0,
    /// </summary>
    /// <param name="fogObj">迷雾的资源对象</param>
    /// <param name="unlockedData">解锁数据</param>
    /// <param name="mapSize">一行多少个格子</param>
    /// <param name="margin">外边格子数量</param>
    public static void Init(GameObject fogObj, bool[] unlockedData, int mapSize, int margin)
    {
        k_Margin = margin;
        //PrintData(unlockedData, mapSize);

        int len = unlockedData.Length;
        int mapHeight = len / mapSize;
        // 为了显示上迷雾覆盖到地图外，需要额外加上margin的数据
        int marginMapWidth = mapSize + margin * 2;
        int marginMapHeight = mapSize + margin * 2;
        int marginLen = marginMapWidth * marginMapHeight;

        byte[] data = new byte[marginLen];
        byte flag_locked = (byte)FogState.Locked;
        byte flag_unlocked = (byte)FogState.Unlocked;

        for (int j = 0; j < marginMapHeight; j++)
        {
            for (int i = 0; i < marginMapWidth; i++)
            {
                int marginIndex = j * marginMapWidth + i;
                if (i >= margin && i < margin + mapSize && j >= margin && j < margin + mapHeight)
                {
                    int index = (j - margin) * mapHeight + (i - margin);
                    
                    // 暂时全部解锁
                    data[marginIndex] = flag_unlocked;     // unlockedData[index] ? flag_unlocked : flag_locked;
                }
                else
                {
                    data[marginIndex] = flag_locked;
                }
            }
        }

        //PrintData(data, marginMapWidth);

        if (fogObj != null)
        {
            FogUtility.InitFog(fogObj, data, marginMapWidth);
        }
    }

    /// <summary>
    /// 打开显示指定坐标的迷雾
    /// </summary>
    /// <param name="x">横坐标</param>
    /// <param name="y">纵坐标</param>
    public static void EnableFog(int x, int y)
    {
        //Debug.LogError(string.Format("xxxxxxxxxxxx  EnableFog {0} {1}", x, y));
        FogUtility.UpdateFogMask(x + k_Margin, y + k_Margin, (int)FogState.Locked);
    }

    /// <summary>
    /// 移除指定坐标的迷雾,移除完成后需要调用FogSystem.Apply
    /// </summary>
    /// <param name="x">横坐标</param>
    /// <param name="y">纵坐标</param>
    /// <param name="playAnim">移除时是否播放动画</param>
    public static void DisableFog(int x, int y, bool playAnim)
    {
        //Debug.LogError(string.Format("xxxxxxxxxxxx  DisableFog {0} {1}", x, y));
        FogUtility.UpdateFogMask(x + k_Margin, y + k_Margin, (int)FogState.Unlocked);
    }

    /// <summary>
    /// 打开迷雾被选中时的高亮
    /// </summary>
    /// <param name="x">横坐标</param>
    /// <param name="y">纵坐标</param>
    public static void EnableFogSelectedHighlight(int x, int y)
    {
        //Debug.LogError(string.Format("xxxxxxxxxxxx  EnableFogSelectedHighlight {0} {1}", x, y));
        FogUtility.UpdateFogMask(x + k_Margin, y + k_Margin, (int)FogState.Selected);
    }

    /// <summary>
    /// 打开迷雾被探索中的高亮
    /// </summary>
    /// <param name="x">横坐标</param>
    /// <param name="y">纵坐标</param>
    public static void EnableFogExploringHighlight(int x, int y)
    {
        //Debug.LogError(string.Format("xxxxxxxxxxxx  EnableFogExploringHighlight {0} {1}", x, y));
        FogUtility.UpdateFogMask(x + k_Margin, y + k_Margin, (int)FogState.Selected);
    }

    /// <summary>
    /// 关闭迷雾所有高亮
    /// </summary>
    /// <param name="x">横坐标</param>
    /// <param name="y">纵坐标</param>
    public static void DisableFogHighlight(int x, int y)
    {
        //Debug.LogError(string.Format("xxxxxxxxxxxx  DisableFogHighlight {0} {1}", x, y));
        FogUtility.UpdateFogMask(x + k_Margin, y + k_Margin, (int)FogState.Locked);
    }

    /// <summary>
    /// 应用关于迷雾的修改
    /// </summary>
    /// <param name="obj"></param>
    public static void Apply(GameObject obj)
    {
        //Debug.LogError(string.Format("xxxxxxxxxxxx  Apply"));
        FogUtility.ApplyFogMask(obj);
    }
}
